<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/5
 * Time: 21:29
 */
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
    private $user;
    public function __construct()
    {
        parent::__construct();
        $this->user=M('user');
    }

    public function register()
    {
        $this->display();
    }

    function do_register()
    {
        $User=D('User');
        if(!$User->create()){
            $this->error($User->getError());
        }else{
            $User->time=time();
            $User->add();
            $this->success('注册成功',U('login'));
        }
    }

    public function login()
    {
        $this->display();
    }

    public function do_login()
    {
        $code = I("post.code");
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }

        $data['email'] = I("post.email");
        $data['password'] = substr(md5(I("post.password")), 6, -6);
        $user = $this->user->where($data)->find();
        if (!$user) {
            $this->error("账号或密码错误");
        }

        if (isset($_POST['rem'])) {
            setcookie('token', $user['token'], time() + 60 * 60 * 24 * 7, '/');
        } else {
            setcookie('token', $user['token'], null, '/');
        }

        $_SESSION['user'] = $user;
        $this->success('登录成功', U("Admin/Index/index"));
    }

    public function verify()
    {
        $Verify = new \Think\Verify(C('verify'));
        $Verify->entry();
    }

    //检测验证码是否正确
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    function logout()
    {
        $_SESSION = array();
       if (isset($_COOKIE[session_name()])) {
           setcookie(session_name(), "", time() - 3600, "/");
           setcookie('token', "", time() - 3600, "/");
        }

        $this->success('您已经安全退出',U('Admin/User/login'));
    }
}
